package com.edu.common.utils;

public class Result {
    private Object data;
    private ResultCode resultCode;

    public Result() {
    }

    public Result(ResultCode resultCode) {
        this.resultCode = resultCode;
    }

    public Result(Object data) {
        this.resultCode = new ResultCode(ResultCode.SUCCESS,ResultCode.DEFAULT_SUCCESS_MESSAGE);
        this.data = data;
    }

    public Result(ResultCode resultCode,Object data) {
        this(resultCode);
        this.data = data;
    }

    /**
     * 响应成功
     * @param data 响应的数据
     * @return 封装的请求
     */
    public static Result success(Object data){
        return new Result(data);
    }

    /**
     * 响应成功
     * @param resultCode 响应状态码
     * @param data 响应数据
     * @return 封装请求
     */
    public static Result success(ResultCode resultCode,Object data){
        return new Result(resultCode,data);
    }

    /**
     * 响应失败
     * @param resultCode 状态码
     * @return 封装请求
     */
    public static Result success(ResultCode resultCode){
        return new Result(resultCode);
    }

    /**
     * 响应失败
     * @param resultCode 状态码
     * @return 封装请求
     */
    public  static Result fail(ResultCode resultCode){
        return new Result(resultCode);
    }

    /**
     * 响应失败
     * @return 封装请求
     */
    public  static Result fail(){
        return new Result(new ResultCode(ResultCode.FAIL,ResultCode.DEFAULT_FAIL_MESSAGE));
    }

    /**
     * 判断请求是否成功
     * @return 请求状态 true or false
     */
    public boolean isSuccess(){
        return ResultCode.SUCCESS == this.resultCode.getCode();
    }

    public <T> T getData(){
        return (T) data;
    }

    public void setData(Object data){
        this.data = data;
    }

    public ResultCode getResultCode() {
        return resultCode;
    }

    public void setResultCode(ResultCode resultCode) {
        this.resultCode = resultCode;
    }
}
